package xcmg.device.web;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.borrowMaterial.BorrowMaterialDO;
import xcmg.device.dao.entity.returnMaterial.ReturnMaterialDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ConstantInfo;
import xcmg.device.service.returnMaterial.ReturnMaterialService;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ZHANGK
 * @since 2020/6/17
 **/
@RestController
@RequestMapping("/returnMaterial")
public class ReturnMaterialController extends BasicController {

    @Autowired
    ReturnMaterialService returnMaterialService;

//    @Value("${spring.file.download.path}")
//    private String filePath;
//
//    @Autowired
//    private ResourceLoader resourceLoader;

    @PostMapping("/list")
    public ActionResult<PageInfo> list(@RequestBody SearchCommonVO<ReturnMaterialDO> condition) {
        return actionValueResult(returnMaterialService.selectPage(condition).getPageInfo());
    }

    @PostMapping("/add")
    public ActionResult add(@RequestBody ReturnMaterialDO returnMaterialDO) throws Exception {
        returnMaterialDO.setOrgId(getOrgId());
        return actionResult(returnMaterialService.insert(returnMaterialDO));
    }

    @PostMapping("/update")
    public ActionResult update(@RequestBody ReturnMaterialDO returnMaterialDO){
        return actionResult(returnMaterialService.updateById(returnMaterialDO));
    }

    @PostMapping("/delete")
    public ActionResult delete(@RequestBody ReturnMaterialDO returnMaterialDO) throws Exception {
        String orgId = getOrgId();
        return actionResult(returnMaterialService.deleteById(returnMaterialDO,orgId));
    }

    @PostMapping("/batchDelete")
    public ActionResult batchDelete(@RequestBody List<ReturnMaterialDO> returnMaterialDOList) throws Exception {
        String orgId = getOrgId();
        return actionResult(returnMaterialService.batchDelete(returnMaterialDOList,orgId));
    }

    @GetMapping("/get")
    public ActionResult<ReturnMaterialDO> get(@RequestParam("id") String id){
        return actionValueResult(returnMaterialService.selectById(id));
    }

    @PostMapping("/audit")
    public ActionResult audit(@RequestBody ReturnMaterialDO returnMaterialDO) throws Exception {
        returnMaterialDO.setReturnStatus(ConstantInfo.RETURN_STATUS_AUDIT);
        //return actionResult(returnMaterialService.audit(returnMaterialDO));
        return actionResult(returnMaterialService.updateById(returnMaterialDO));
    }

    @PostMapping("/refuse")
    public ActionResult refuse(@RequestBody ReturnMaterialDO returnMaterialDO){
        returnMaterialDO.setReturnStatus(ConstantInfo.RETURN_STATUS_REFUSE);
        return actionResult(returnMaterialService.updateById(returnMaterialDO));
    }

    @PostMapping("/getInsertList")
    public ActionResult getInsertList(@RequestBody SearchCommonVO<BorrowMaterialDO> condition){
        return actionValueResult(returnMaterialService.getInsertList(condition).getPageInfo());
    }

    @PostMapping("/uploadImage")
    public ActionResult uploadImage(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String billId = request.getParameter("x-billId");
        return actionResult(returnMaterialService.uploadImage(billId, file));
    }

//    @PostMapping("/uploadObsImage")
//    public ActionResult uploadObsImage(HttpServletRequest request) throws Exception {
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        MultipartFile file = multipartRequest.getFile( multipartRequest.getFileNames().next());
//        String billId = request.getParameter("x-billId");
//        return actionResult(returnMaterialService.uploadObsImage(billId,file));
//    }

//    @GetMapping("/image/**")
//    public ResponseEntity<?> showImage(HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {
//        String url = request.getRequestURI();
//        String imgName = url.substring(url.indexOf("/image/")+7);
//        InputStream inputStream = new FileInputStream(new File(filePath+"returnMaterialImage/"+imgName));
//        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
//        HttpHeaders httpHeaders = new HttpHeaders();
//        response.setContentType("image/png;charset=UTF-8");
//        return ResponseEntity.ok(inputStreamResource);
//    }

    @PostMapping("/batchInsert")
    public ActionResult batInsert(@RequestBody List<BorrowMaterialDO> borrowMaterialDOList) throws Exception {
        String orgId = getOrgId();
        String createdId = getUserId();
        return actionResult(returnMaterialService.batchInsert(borrowMaterialDOList,orgId,createdId));
    }

    @GetMapping("/getPrintList")
    public ActionResult getPrintList(String[] ids){
        return actionValueResult(returnMaterialService.selectPrintList(ids));
    }

    @PostMapping("/finish")
    public ActionResult finish(@RequestBody ReturnMaterialDO returnMaterialDO) throws Exception {
        returnMaterialDO.setReturnStatus(ConstantInfo.RETURN_STATUS_FINISH);
        return actionResult(returnMaterialService.finish(returnMaterialDO));
    }

}
